import { RoomResultDao } from "../../hall_server/dao/RoomResultDao";
import { GameResult } from "../../hall_server/model/GameResult";
import { RoomModel } from "../../hall_server/model/RoomModel";
import { RoomPlayer } from "../../hall_server/model/RoomPlayer";
import { LogServerHelper } from "../../log_server/LogServerHelper";
import { SocketConnection } from "../engine/socketserver/socket/SocketConnection";
import { SocketConnectionManager } from "../engine/socketserver/socket/SocketConnectionManager";
import DateUtil from "../engine/utils/DateUtil";
import RandomUtil from "../engine/utils/RandomUtil";
import { ResponseModel } from "../model/ResponseModel";
import { RoomManager } from "./RoomManager";

export class GameManager {

    public static instance: GameManager = new GameManager();

    private create_game_result(room: RoomModel) {
        let gameResult: GameResult = room.gameResult
        gameResult.gameType = room.gameType
        gameResult.room_id = room.room_id
        gameResult.clubid = room.clubid;
        gameResult.gameConf = room.conf
        gameResult.playerList = room.playerList;
        gameResult.time = DateUtil.now()
        let bigWinScore: number = 0;
        let bigWinList: Array<number> = []
        for (let i = 0; i < gameResult.playerList.length; i++) {
            let player: RoomPlayer = gameResult.playerList[i]
            if (player.score > bigWinScore) {
                bigWinScore = player.score;
                bigWinList = [player.userid]
            }
            else if (player.score == bigWinScore) {
                bigWinList.push(player.userid);
            }
            if (i == 0) {
                gameResult.userid1 = player.userid
            }
            else if (i == 1) {
                gameResult.userid2 = player.userid
            }
            else if (i == 2) {
                gameResult.userid3 = player.userid
            }
            else if (i == 3) {
                gameResult.userid4 = player.userid
            }
            else if (i == 4) {
                gameResult.userid5 = player.userid
            }
        }
        gameResult.bigWin = RandomUtil.randomItem(bigWinList);
        RoomManager.instance.decRoomDayingjiaCost(room, gameResult.bigWin)
        return gameResult
    }

    public async add_game_result(room: RoomModel) {
        let gameResult: GameResult = this.create_game_result(room)
        if (gameResult.id > 0) {
            await RoomResultDao.update_room_result(gameResult)
        } else {
            gameResult.id = await RoomResultDao.insert_room_result(gameResult)
        }
    }

    public async notify_game_result(room: RoomModel) {
        let gameResult: GameResult = this.create_game_result(room)
        let endModel: ResponseModel = new ResponseModel()
        let endObj: object = {
            "gameResult": gameResult,
        }
        endModel.data = endObj
        for (let i = 0; i < gameResult.playerList.length; i++) {
            let player: RoomPlayer = room.playerList[i]
            if (player) {
                let conn: SocketConnection = SocketConnectionManager.instance.getSocketConnectionByUserid(player.userid)
                if (conn) {
                    // conn.createMsgToClient(0, ProtoConfig.notify_game_result, endModel)
                }
                if (gameResult.clubid > 0) {
                    let wintimes: number = 0
                    if (player.score > 0) {
                        wintimes++
                    }
                    LogServerHelper.log_club_top(gameResult.clubid, player.userid, player.score, wintimes, 1)
                }
            }
        }
        if (gameResult.id > 0) {
           await RoomResultDao.update_room_result(gameResult)
        } else {
            gameResult.id = await RoomResultDao.insert_room_result(gameResult)
        }
        return gameResult
    }

}